<template>
  <div class="marketList">
    <el-card shadow="always" :body-style="{ padding: '20px' }">
      <div slot="header">
        <el-button v-btn-permi:add type="primary" size="default" @click="add">新增</el-button>
      </div>
      <div class="table-holder">
        <!-- 展示表格 -->
        <el-table :data="advertiseList" border style="width: 100%">
          <el-table-column fixed="left" type="index" label="序号" width="50" />
          <el-table-column label="广告图片" width="200">
            <template slot-scope="scope">
              <img class="table-image" :src="scope.row.pic" alt="">
            </template>
          </el-table-column>
          <el-table-column label="时间" width="300">
            <template slot-scope="scope">
              <p>开始时间：{{ scope.row.startTime }}</p>
              <p>结束时间：{{ scope.row.endTime }}</p>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="广告名称" width="200" />
          <el-table-column prop="clickCount" label="点击数" width="200" />
          <el-table-column prop="orderCount" label="下单数" width="200" />
          <el-table-column label="轮播位置" width="200">
            <template slot-scope="scope">
              <p v-if="scope.row.type === 0">首页轮播</p>
              <p v-else>其他位置</p>
            </template>
          </el-table-column>
          <el-table-column prop="url" label="链接地址" width="200" />
          <el-table-column prop="note" label="备注" width="200" />
          <el-table-column label="上下线状态" width="200">
            <template slot-scope="scope">
              <p v-if="scope.row.status === 0">下线</p>
              <p v-if="scope.row.status === 1">上线</p>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="200">
            <template slot-scope="scope">
              <el-button
                v-btn-permi:edit
                type="text"
                size="small"
                @click="edit(scope.row)"
              >编辑</el-button>
              <el-button
                v-btn-permi:remove
                type="text"
                style="color: red"
                size="small"
                @click="remove(scope.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>

    <div class="pop">
      <el-dialog title="广告详情" width="65%" :visible.sync="dialogVisible">
        <div class="form-box">
          <el-form
            ref="adForm"
            size="mini"
            :model="adInfo"
            :rules="rules"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-row :gutter="20">
              <el-col :span="12" :offset="0">
                <el-form-item label="广告名称" prop="name">
                  <el-input v-model="adInfo.name" />
                </el-form-item>
              </el-col>
              <el-col :span="12" :offset="0">
                <el-form-item label="备注" prop="note">
                  <el-input v-model="adInfo.note" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12" :offset="0">
                <el-form-item label="排序" prop="sort ">
                  <el-input v-model="adInfo.sort" />
                </el-form-item>
              </el-col>
              <el-col :span="12" :offset="0">
                <el-form-item label="产品数量" prop="productCount ">
                  <el-input v-model="adInfo.productCount" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12" :offset="0">
                <el-form-item label="链接" prop="url ">
                  <el-input v-model="adInfo.url" />
                </el-form-item>
              </el-col>
              <el-col :span="12" :offset="0">
                <el-form-item label="首页轮播位置" prop="orderCount ">
                  <el-select v-model="adInfo.type" placeholder="请选择">
                    <el-option
                      v-for="item in typeOptions"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12" :offset="0">
                <el-form-item label="开始时间">
                  <el-date-picker
                    v-model="adInfo.startTime"
                    type="datetime"
                    placeholder="选择日期时间"
                    align="right"
                    value-format="yyyy-MM-dd HH:mm:ss"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12" :offset="0">
                <el-form-item label="结束时间">
                  <el-date-picker
                    v-model="adInfo.endTime"
                    type="datetime"
                    placeholder="选择日期时间"
                    align="right"
                    value-format="yyyy-MM-dd HH:mm:ss"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12" :offset="0">
                <el-form-item label="上下线状态" prop="status">
                  <el-switch
                    v-model="adInfo.status"
                    :active-value="1"
                    :inactive-value="0"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12" :offset="0">
                <el-form-item label="图片" prop="coverImg">
                  <el-upload
                    class="avatar-uploader"
                    :action="uploadUrl"
                    :headers="uploadHeaders"
                    :show-file-list="false"
                    :on-success="uploadSuccess"
                  >
                    <img v-if="adInfo.pic" :src="adInfo.pic" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon" />
                  </el-upload>
                </el-form-item>
              </el-col>
            </el-row>

            <el-form-item>
              <el-button type="primary" @click="submit">提交</el-button>
              <el-button @click="reset">取消</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  requestAdvertiseList,
  addAdvertise,
  deleteAdvertise,
  changeAdvertise
} from '@/api/market'

import mixin from '@/mixins/index'
export default {
  mixins: [mixin],
  data() {
    return {
      // 广告信息
      adInfo: {
        clickCount: 0,
        createTime: '',
        endTime: '',
        id: '',
        modifyTime: '',
        name: '',
        note: '',
        orderCount: 0,
        pic: '',
        productId: '',
        sort: 0,
        startTime: '',
        status: 0,
        type: 0,
        url: '',
        productCount: ''
      },
      rules: {
        name: [{ required: true, message: '请输入广告名称', trigger: 'blur' }]
      },
      // 首页轮播位置下拉选项
      typeOptions: [
        {
          value: 0,
          label: 'app首页轮播'
        },
        {
          value: 1,
          label: '其他'
        }
      ],
      radio: [],
      // 广告列表
      advertiseList: [],
      // 弹出框展示
      dialogVisible: false,
      isCreate: false
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 点击新建按钮
    add() {
      this.resetData()
      this.isCreate = true
      this.dialogVisible = true
    },
    // 获取广告列表信息
    getData() {
      requestAdvertiseList().then((res) => {
        console.log(res)
        this.advertiseList = res.data.items
      })
    },
    // 编辑
    edit(item) {
      this.adInfo = { ...item }
      this.dialogVisible = true
      this.isCreate = false
    },
    // 上传图片成功钩子
    uploadSuccess(res) {
      console.log(res)
      if (res.code === 20000) {
        this.adInfo.pic = res.data.fileUrl
      }
    },
    // 添加广告请求
    addAdvertiseMethod() {
      addAdvertise(this.adInfo).then((res) => {
        console.log(res)
        if (res.code === 20000) {
          this.$message({
            type: 'success',
            message: '修改广告成功'
          })
          this.dialogVisible = false
          this.getData()
        } else {
          this.$message({
            type: 'error',
            message: '修改广告失败'
          })
        }
      })
    },
    // 编辑广告请求
    changeAdvertiseMethod() {
      changeAdvertise(this.adInfo).then((res) => {
        console.log(res)
        if (res.code === 20000) {
          this.$message({
            type: 'success',
            message: '上传广告成功'
          })
          this.dialogVisible = false
          this.getData()
        } else {
          this.$message({
            type: 'error',
            message: '上传广告失败'
          })
        }
      })
    },
    // 提交
    submit() {
      console.log(this.adInfo)
      if (this.isCreate) {
        this.addAdvertiseMethod()
      } else {
        this.changeAdvertiseMethod()
      }
    },
    // 重置数据
    resetData() {
      this.adInfo = {
        clickCount: 0,
        createTime: '',
        endTime: '',
        id: '',
        modifyTime: '',
        name: '',
        note: '',
        orderCount: 0,
        pic: '',
        productId: '',
        sort: 0,
        startTime: '',
        status: 0,
        type: 0,
        url: '',
        productCount: ''
      }
    },
    // 点击取消按钮
    reset() {
      this.resetData()
      this.dialogVisible = false
    },
    // 删除
    remove(id) {
      console.log(id)
      const _this = this
      this.$confirm('此操作将永久删除该广告, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteAdvertise(id).then((res) => {
            // console.log(res);
            if (res.code === 20000) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              _this.getData()
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style lang="scss" scoped>
.marketList {
  .table-image {
    width: 100px;
    height: 100px;
  }

  .image {
    width: 100px;
    height: 100px;
  }

  .form-box {
    padding: 0 50px;
  }
}
</style>
